﻿//图标
var jiconlist, winicons, currentComboBox;

$(function () {
    $("#menuType").ligerComboBox({
        cancelable: false,
        width: 190,
        valueFieldID: 'hdnMenuType',
        onSelected: function (value) {
            if (value == 0) {
                $("#divDisplayStyle").show();
            }
            else
            {
                $("#divDisplayStyle").hide();
            }
        }
    });

    $("#parentId").ligerComboBox({
        width: 250,
        selectBoxWidth: 250,
        selectBoxHeight: 200,
        valueField: 'MenuId',
        textField: 'MenuName',
        valueFieldID: 'hdnParentId',
        treeLeafOnly: false,
        tree: {
            url: '/Sysbase/Menu/GetParentMenu?ram' + Math.random(),
            ajaxType: 'get',
            idFieldName: "MenuId",
            parentIDFieldName: "ParentId",
            textFieldName: "MenuName",
            single: true,
            onSuccess: function (data) {
                $(".l-tree").css('width', '250px');
                if ($("#parentId").val() != "0" && $("#parentId").val() != "") {
                    $("#parentId").ligerComboBox().setValue($("#parentId").val());
                }
                else {
                    $("#parentId").val('');
                }
            }
        }
    });

    $("#txtImgUrl").bind("dblclick", f_selectContact);
    jiconlist = $("body > .iconlist:first");
    if (!jiconlist.length) jiconlist = $('<ul class="iconlist"></ul>').appendTo('body');
});

function f_selectContact() {
    if (winicons) {
        winicons.show();
        $(".l-dialog").hide();
    }

    winicons = $.ligerDialog.open({
        title: '选取图标',
        target: jiconlist,
        width: 400, height: 250, modal: true
    });
    if (!jiconlist.attr("loaded")) {
        $.ajax({
            url: "/Sysbase/Menu/GetIcons",
            type: "get",
            async: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                var obj = eval(data);
                for (var i = 0, l = obj.length; i < l; i++) {
                    var src = obj[i];
                    var reg = /(images\\newIcon)(.+)/;
                    var match = reg.exec(src);
                    jiconlist.append("<li><img src='../../Content/images/newIcon/" + src.filename + "' /></li>");
                    if (!match) continue;
                }
                jiconlist.attr("loaded", true);
            }
        });
    }

    $(".iconlist li").live('mouseover', function () {
        $(".iconlist li").removeClass("over");
        $(this).addClass("over");
    }).live('mouseout', function () {
        $(this).removeClass("over");
    }).live('click', function () {
        if (!winicons) return;
        var src = $("img", this).attr("src");
        $("#txtImgUrl").val(src.split('newIcon/')[1]);
        winicons.close();
    });
}

function save() {
    var id = $("#hdnMenuId").val();
    var menuType = $("#hdnMenuType").val();
    var name = $("#txtName").val();
    var menuId = $("#txtMenuId").val();
    var path = $("#txtPath").val();
    var imgUrl = $("#txtImgUrl").val();
    var parentId = $("#hdnParentId").val();
    var isVisible = $("#chkIsVisible").prop('checked') ? 0 : 1;
    var istab = $("#chkIstab").prop('checked') ? 1 : 0;
    var sort = $("#txtSort").val();
    var remarks = $("#txtRemarks").val();
    var displayStyle = 0;
    if (menuType == 0) {
        if ($("#chkDisplayStyle").prop('checked')) {
            displayStyle = 2;
        }
        else {
            displayStyle = 1;
        }
    }

    if (IsEmpty(name)) {
        $.ligerDialog.error("菜单名称不能为空");
        $("#txtName").focus();
        return;
    }

    if (IsEmpty(menuId)) {
        $.ligerDialog.error("菜单编码不能为空");
        $("#txtMenuId").focus();
        return;
    }

    $.ajax({
        url: "/Sysbase/Menu/Save",
        type: "post",
        data: {
            Id: id,
            MenuName: name,
            MenuId: menuId,
            MenuType: menuType,
            MenuPath: path,
            ImgUrl: imgUrl,
            ParentId: parentId,
            IsVisible: isVisible,
            Istab: istab,
            Sort: sort,
            Remarks: remarks,
            DisplayStyle: displayStyle
        },
        success: function (r) {
            var r = $.parseJSON(r);
            if (r.Result == 1) {
                parent.$("#btnSearch").click();
                closeDialog();
            }
            else if (r.Result == 0) {
                $.ligerDialog.error(r.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        }
    });
}

function closeDialog() {
    frameElement.dialog.close();//关闭dialog
}